package com.smart.controller.sys;

import java.util.List;
import com.smart.entity.SysRole;
import javax.annotation.Resource;
import com.smart.util.CommonPage;
import io.swagger.annotations.Api;
import com.smart.util.CommonResult;
import com.smart.service.RoleService;
import com.smart.service.UserService;
import io.swagger.annotations.ApiOperation;
import com.smart.common.annotation.SysLog;
import com.smart.common.enums.BusinessType;
import com.smart.controller.BaseController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author lgb
 * @date 2020年6月11日 下午3:48:43
 * Copyright (C) 深圳工勘科技
 * @说明: 角色管理
 **/
@RestController
@RequestMapping("/sys/role")
@Api(tags="Sys-role => 角色管理")
public class RoleController extends BaseController{
	@Resource
	private UserService userService;
	@Resource
	private RoleService roleService;
	
	@ApiOperation("list => 根据名获取角色列表")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<SysRole>> list(SysRole sysRole,
          @RequestParam(value="pageSize") Integer pageSize,
          @RequestParam(value="pageNum") Integer pageNum) {
        IPage<SysRole> roles = roleService.getPage(sysRole, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(roles));
    }
	
	@ApiOperation("listAll => 获取所有角色列表")
    @GetMapping(value = "/listAll")
    public CommonResult<List<SysRole>> listAll() {
        List<SysRole> roles = roleService.getListAll();
        return CommonResult.success(roles);
    }
	
	@ApiOperation("创建指定角色信息")
    @SysLog(title ="角色管理",businessType=BusinessType.INSERT)
    @PostMapping("create")
    public CommonResult<Integer> create(@RequestBody SysRole sysRole) {
        int count = roleService.create(sysRole);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	@ApiOperation("delete/{ids} => 删除指定角色信息")
    @SysLog(title="角色管理",businessType = BusinessType.DELETE)
    @DeleteMapping(value="/delete/{ids}")
    public CommonResult<Integer> delete(@PathVariable Long[] ids) {
		if(ids.length >= 1){
			String names=userService.findNameByUserId(ids[0]);
			if(StringUtils.isNotEmpty(names)){
				return CommonResult.failed("该角色下有人员绑定!");
			}
		}
        int count = roleService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	@ApiOperation("/{id} => 根据角色ID获取详细信息")
    @GetMapping("/{id}")
    public CommonResult<SysRole> info(@PathVariable("id") Long id) {
        SysRole role = roleService.getById(id);
        return CommonResult.success(role);
    }
	
	@ApiOperation("update/{id} => 修改指定角色信息")
    @SysLog(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id,
    		@RequestBody SysRole sysRole) {
        int count = roleService.update(id, sysRole);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
}